package org.gp.Interfaz;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.gp.tringes.GestorIGAnadirCamada;
import org.gp.tringes.GestorIGAnadirTrineo;
import org.gp.tringes.GestorIGInicio;

import java.awt.GridLayout;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

public class IGAnadirTrineo extends JDialog {
	
	private Integer idClub;
	
	private JPanel panel;
	private JLabel lblId;
	private JTextField txtId;
	private JLabel lblFabr;
	private JTextField txtFabr;
	private JLabel lblFechaFab;
	private JTextField txtFechaFab;
	private JLabel lblInvisible;
	
	private JPanel panel_1;
	private JButton btnAnadir;
	private JButton btnCancelar;
	
	private JPanel panel_2;
	private JLabel lblFormato;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IGAnadirTrineo dialog = new IGAnadirTrineo(0);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
					dialog.setModal(true);
					dialog.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public IGAnadirTrineo(Integer pIdClub) {
		idClub = pIdClub;
		
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		//ID-56
		setTitle(GestorIGInicio.getGestorIGInicio().getTexto(56));
		setBounds(100, 100, 400, 250);
		
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.CENTER);
		getContentPane().add(getPanel_1(), BorderLayout.SOUTH);
		getContentPane().add(getPanel_2(), BorderLayout.NORTH);
		
	}
	
	private JPanel getPanel(){
		if(panel == null){
			panel = new JPanel();
			
			panel.setLayout(new GridLayout(4, 2, 0, 15));
			
			panel.add(getLblId());
			panel.add(getTxtId());
			panel.add(getLblFabr());		
			panel.add(getTxtFabr());
			panel.add(getLblFechaFab());
			panel.add(getTxtFechaFab());
			panel.add(getLblInvisible());
			panel.add(getLblFormato());
		}
		return panel;
	}
	private JLabel getLblId(){
		if(lblId == null){
			//ID-57
			lblId = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(57));
			lblId.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblId;
	}
	private JTextField getTxtId(){
		if(txtId == null){
			txtId = new JTextField();
			txtId.setColumns(10);
		}
		return txtId;
	}
	private JLabel getLblFabr(){
		if(lblFabr == null){
			//ID-58
			lblFabr = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(58));
			lblFabr.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblFabr;
	}
	private JTextField getTxtFabr(){
		if(txtFabr == null){
			txtFabr = new JTextField();
			txtFabr.setColumns(10);
		}
		return txtFabr;
	}
	private JLabel getLblFechaFab(){
		if(lblFechaFab == null){
			//ID-59
			lblFechaFab = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(59));
			lblFechaFab.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblFechaFab;
	}
	private JTextField getTxtFechaFab(){
		if(txtFechaFab == null){
			txtFechaFab = new JTextField();
			txtFechaFab.setColumns(10);
		}
		return txtFechaFab;
	}
	private JLabel getLblInvisible(){
		if(lblInvisible == null){
			lblInvisible = new JLabel("");
			lblInvisible.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblInvisible;
	}
	private JLabel getLblFormato() {
		if (lblFormato == null) {
			//ID-18
			lblFormato = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(18));
			lblFormato.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblFormato;
	}
	
	private JPanel getPanel_1(){
		if(panel_1 == null){
			panel_1 = new JPanel();
			panel_1.add(getBtnAnadir()); 
			panel_1.add(getBtnCancelar());
		}
		return panel_1;
	}
	private JButton getBtnAnadir(){
		if(btnAnadir == null){
			//ID-29
			btnAnadir = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(29));
			btnAnadir.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					try{
						Boolean longMatr = GestorIGAnadirTrineo.getGestorIGAnadirTrineo().comprobarLongitudMatr(txtId.getText());
						if(longMatr){
							Boolean longFabr = GestorIGAnadirTrineo.getGestorIGAnadirTrineo().comprobarLongitudFabr(txtFabr.getText());
							if(longFabr){
								Boolean expCorrecta = GestorIGAnadirCamada.getGestorIGAnadirCamada().comprobarExpresionFecha(txtFechaFab.getText());
								if(expCorrecta){
									Boolean existeTrineo = GestorIGAnadirTrineo.getGestorIGAnadirTrineo().existeTrineo(txtId.getText());
									if(!existeTrineo){
										//--Con esto damos la fecha de tipo String en tipo Date
										java.sql.Date fecFormatoDate = null;
										try {
											SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd", new Locale("es", "ES"));
											fecFormatoDate = new java.sql.Date(sdf.parse(txtFechaFab.getText()).getTime());
										} catch (Exception ex) {
											System.out.println("Error al obtener el formato de la fecha/hora: " + ex.getMessage());
										}
										//--
										
										GestorIGAnadirTrineo.getGestorIGAnadirTrineo().insertarTrineo(txtId.getText(), txtFabr.getText(), fecFormatoDate, idClub);
										//ID-21 y 6
										JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(21), GestorIGInicio.getGestorIGInicio().getTexto(6), JOptionPane.INFORMATION_MESSAGE);
										dispose();
									}else{
										//ID-60 y 4
										JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(60), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
									}
								}else{
									//ID-38 y 4
									JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(38), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
								}
							}else{
								//ID-61 y 4
								JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(61), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
							}
						}else{
							//ID-62 y 4
							JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(62), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
						}
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
		}
		return btnAnadir;
	}
	private JButton getBtnCancelar(){
		if(btnCancelar == null){
			//ID-8
			btnCancelar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(8));
			btnCancelar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					dispose();
				}
			});
		}
		return btnCancelar;
	}
	
	private JPanel getPanel_2(){
		if(panel_2 == null){
			panel_2 = new JPanel();
		}
		return panel_2;
	}
}
